package com.team.sion.controller;

import com.team.sion.common.SuccessMessage;
import com.team.sion.common.WarningMessage;
import com.team.sion.pojo.Consumer;
import com.team.sion.pojo.Singer;
import com.team.sion.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/singer")
public class SingerController extends BaseController{
    @Autowired
    private SingerService singerService;
    @RequestMapping("")
    public Object singerList(){
        List<Singer> list = singerService.findAll();
        return new SuccessMessage<List<Singer>>("singer",list).getMessage();
    }
    @RequestMapping("/sex/detail")
    public Object singerDetail(String sex){
        List<Singer> list = singerService.findBySex(sex);
        return new SuccessMessage<List<Singer>>("singerDetail",list).getMessage();
    }

    @RequestMapping("/add")
    public Object addSinger(Singer singer){
        boolean flag = singerService.addSinger(singer);
        if(flag){
            return new SuccessMessage<>("add").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/update")
    public Object updateSinger(Singer singer){
        boolean flag = singerService.updateSinger(singer);
        if(flag){
            return new SuccessMessage<>("update").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

    @RequestMapping("/avatar/update")
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile,
                                   @RequestParam("id") Integer id,
                                   HttpServletRequest request) throws Exception {
        // ⼀.定义⽂件名
        String fileName = "";
        // 1.获取原始⽂件名
        String uploadFileName = avatorFile.getOriginalFilename();
        // 2.截取⽂件扩展名
//        String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1);
        // 3.把⽂件加上随机数，防⽌⽂件重复
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 4.判断是否输⼊了⽂件名
//        if (!StringUtils.isEmpty(""+id)) {
//            fileName = uuid + "_" + id + "." + extendName;
//        } else {
//            fileName = uuid + "_" + uploadFileName;
//        }
//        System.out.println(fileName);
        fileName = uuid + "_" + uploadFileName;
        // ⼆.获取⽂件路径
        ServletContext context = request.getServletContext();
        String basePath = context.getRealPath("/img/singerPic");
        // 三.解决同⼀⽂件夹中⽂件过多问题
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).replaceAll("-","");
        // 四.判断路径是否存在
        File file = new File(basePath + "/" + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String singPic = "img/singerPic"+"/"+datePath+"/"+fileName;
        singerService.updatePic(id,singPic);
        // 5.使⽤ MulitpartFile 接⼝中⽅法，把上传的⽂件写到指定位置
        avatorFile.transferTo(new File(file, fileName));
        return new SuccessMessage<String>("success",singPic).getMessage();
    }

    @RequestMapping("/delete")
    public Object deleteSinger(Integer id){
        boolean flag = singerService.deleteSinger(id);
        if(flag){
            return new SuccessMessage<>("delete").getMessage();
        }
        return new WarningMessage("failed").getMessage();
    }

}
